package com.biboheart.breport.dataset.report;

import com.biboheart.breport.core.BrContext;
import com.biboheart.breport.dataset.BrDataSet;
import com.biboheart.breport.dataset.data.BrCol;
import com.biboheart.breport.dataset.data.BrDataSetSimple;
import com.biboheart.breport.template.BrTemplateNode;
import com.biboheart.breport.template.BrTemplateStruct;
import com.biboheart.brick.utils.CheckUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BrReportDefault extends BrReportProjectorAbstract {
    private final Map<String, BrCol> colHash = new HashMap<>();
    private final List<BrCol> colList = new ArrayList<>();
    private final BrDataSet dataSet = new BrDataSetSimple();
    private final BrContext context;

    public BrReportDefault(BrContext context) {
        this.context = context;
    }

    @Override
    public void projection(Map<String, Object> data) {
        dataSet.addData(context, data);
    }

    @Override
    public void generateCols(BrTemplateStruct templateStruct) {
        List<BrTemplateNode> colNodeList = templateStruct.getCols();
        if (CheckUtils.isEmpty(colNodeList)) {
            return;
        }
        for (BrTemplateNode colNode : colNodeList) {
            BrCol col = colHash.get(colNode.getSn());
            if (null == col) {
                col = new BrCol();
                col.setSn(colNode.getSn());
                col.setName(colNode.getName());
                col.setWidth(colNode.getWidth());
                col.setAlign(colNode.getAlign());
                col.setType(colNode.getType());
                colList.add(col);
                colHash.put(colNode.getSn(), col);
            } else {
                col.setName(colNode.getName());
            }
        }
        context.setColHash(colHash);
    }

    @Override
    public List<BrCol> getCols() {
        return colList;
    }

    @Override
    public List<Map<String, Object>> getContent() {
        return dataSet.extract();
    }
}
